package com.mp.approvalreq.dao.impl;

import com.mp.approvalreq.dao.RelateHistoryDao;
import com.mp.approvalreq.entity.Query;
import com.mp.approvalreq.entity.RelateHistory;
import com.plusmoney.util.SQLLogger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: approval-parent
 * @description
 * @author: xu1
 * @create: 2020-08-26 14:04
 **/
public class RelateHistoryDaoImpl implements RelateHistoryDao {
    private static final Logger logger = LoggerFactory.getLogger(RelateHistoryDaoImpl.class);
    private Connection conn;

    public RelateHistoryDaoImpl(Connection conn) {
        this.conn = conn;
    }

    @Override
    public void insertApprovalRelate(RelateHistory relateHistory) throws SQLException {
        String sql = "INSERT into sys_approval_relation_history SET"
                + " company_id = ?, contact_id = ?, request_id = ?, object_type = ?, object_id = ?,"
                + " modify_type = ?, modify_time = now()";

        try (PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, relateHistory.getCompanyId());
            ps.setInt(2, relateHistory.getContactId());
            ps.setInt(3, relateHistory.getRequestId());
            ps.setInt(4, relateHistory.getObjectType());
            ps.setString(5, relateHistory.getObjectId());

            ps.setInt(6, relateHistory.getModifyType());

            logger.info(SQLLogger.logSQL(sql, relateHistory.getCompanyId(), relateHistory.getContactId(), relateHistory.getRequestId(),
                    relateHistory.getObjectId(), relateHistory.getModifyType(), relateHistory.getRequestId(), relateHistory.getModifyType()));
            ps.executeUpdate();
        }

    }

    @Override
    public List<RelateHistory> getRelateHistory(Query query) throws SQLException {
        List<RelateHistory> list = new ArrayList<>();
        String sql = "SELECT company_id, contact_id, request_id, object_type, object_id,"
                + " modify_time, modify_type"
                + " FROM sys_approval_relation_history "
                + " WHERE company_id = ? and request_id = ? order by modify_time desc";
        try (PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, query.getCompanyId());
            ps.setInt(2, query.getRequestId());
            logger.info(SQLLogger.logSQL(sql, query.getCompanyId(), query.getRequestId()));

            try (ResultSet rs = ps.executeQuery()) {
                while (rs.next()) {
                    RelateHistory rh = new RelateHistory();
                    rh.setCompanyId(rs.getInt(1));
                    rh.setContactId(rs.getInt(2));
                    rh.setRequestId(rs.getInt(3));
                    rh.setObjectType(rs.getInt(4));
                    rh.setObjectId(rs.getString(5));

                    rh.setModifyTime(rs.getString(6));
                    rh.setModifyType(rs.getInt(7));

                    list.add(rh);
                }
            }
        }
        return list;
    }
}
